import {
  getBreadCrumbList,
  setTagNavListInLocalstorage,
  getMenuByRouter,
  getTagNavListFromLocalstorage,
  getHomeRoute,
  getNextRoute,
  routeHasExist,
  routeEqual,
  getRouteTitleHandled,
  localSave,
  localRead
} from '@/libs/util'
// import { saveErrorLogger } from '@/api/data'
import router from '@/router'
import routers from '@/router/routers'
import config from '@/config'
import https from '@/js/http.js'

const {homeName} = config

const closePage = (state, route) => {
  const nextRoute = getNextRoute(state.tagNavList, route)
  state.tagNavList = state.tagNavList.filter(item => {
    return !routeEqual(item, route)
  })
  router.push(nextRoute)
}

export default {
  state: {
    breadCrumbList: [],
    tagNavList: [],
    homeRoute: {},
    local: localRead('local'),
    errorList: [],
    hasReadErrorPage: false,
    faqChangeNum: 0,
    givingNum: 0,
    showPasswordFlag: false, // 全局展示输入密码框
    countryList: [],
    areaCountryList: { // 各区域页面的国家列表
      Turkey: [],
      BA: [],
      NA: [],
      WA: [],
      SA: [],
      EA: [],
      nonLocalPrint: [], // 非土印地区
      taiyue: [], // 泰越区
    },
    permissionsCountries: [],//数据区域权限国家
    permissionsPage: [],//页面的权限
    newMenuList: [],
    tabNum:0,


  },
  getters: {
    menuList: (state, getters, rootState) => getMenuByRouter(routers, rootState.user.access),
    errorCount: state => state.errorList.length,
    showPasswordFlag: state => state.showPasswordFlag,
    getCountryList: state => state.countryList,
    getAreaCountryList: state => state.areaCountryList,
    getNewMenuList: (state) => state.newMenuList,
    getPermissionsCountries: (state) => state.permissionsCountries,
    getpermissionsPage: (state) => state.permissionsPage,
    getTabNum: (state) => state.tabNum,
  },
  mutations: {
    settabNum(state,page){
      state.tabNum=state.tabNum+1;

    },
    setpermissionsPage(state, text) {
      state.permissionsPage = text
    },
    setCountryArr(state, text) {
      state.permissionsCountries = text;
    },
    faqChagee(state) {
      state.faqChangeNum = state.faqChangeNum + 1
    },
    changeGiving(state) {
      state.givingNum = state.givingNum + 1
    },
    setBreadCrumb(state, route) {
      state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
    },
    setHomeRoute(state, routes) {
      state.homeRoute = getHomeRoute(routes, homeName)
    },
    setTagNavList(state, list) {
      let tagList = []
      if (list) {
        tagList = [...list]
      } else tagList = getTagNavListFromLocalstorage() || []
      if (tagList[0] && tagList[0].name !== homeName) tagList.shift()
      let homeTagIndex = tagList.findIndex(item => item.name == homeName)
      if (homeTagIndex > 0) {
        let homeTag = tagList.splice(homeTagIndex, 1)[0]
        tagList.unshift(homeTag)
      }
      state.tagNavList = tagList
      setTagNavListInLocalstorage([...tagList])
    },
    closeTag(state, route) {
      let tag = state.tagNavList.filter(item => routeEqual(item, route))
      route = tag[0] ? tag[0] : null
      if (!route) return
      closePage(state, route)
    },
    addTag(state, {route, type = 'unshift'}) {
      let router = getRouteTitleHandled(route)
      if (!routeHasExist(state.tagNavList, router)) {
        if (type == 'push') state.tagNavList.push(router)
        else {
          if (router.name == homeName) state.tagNavList.unshift(router)
          else state.tagNavList.splice(1, 0, router)
        }
        setTagNavListInLocalstorage([...state.tagNavList])
      }
    },

    addError(state, error) {
      state.errorList.push(error)
    },
    setHasReadErrorLoggerStatus(state, status = true) {
      state.hasReadErrorPage = status
    },
    setShowPasswordFlag(state, status) {
      state.showPasswordFlag = status;
    },
    setCountryList(state, countryList) {
      state.countryList = countryList;
    },

    setAreaCountryList(state, areaCountryList) {
      state.areaCountryList = areaCountryList;
    },
    setNewMenuList(state, newMenuList) {
      state.newMenuList = newMenuList;
    },
  },
  actions: {
    addErrorLog({commit, rootState}, info) {
      if (!window.location.href.includes('error_logger_page')) commit('setHasReadErrorLoggerStatus', false)
      const {user: {token, userId, userName}} = rootState
      let data = {
        ...info,
        time: Date.parse(new Date()),
        token,
        userId,
        userName
      }
      // saveErrorLogger(info).then(() => {
      //   commit('addError', data)
      // })
    },
    async getCountryList({commit, state}) {
      if (state.countryList.length > 1) {
        return state.countryList;
      }
      const reqCountryList = await https.findCountryList({}, {});
      const reqList = reqCountryList.data || [];
      reqList.unshift({
        country_name: 'All',
        id: 0,
      })
      commit('setCountryList', reqList);
      setAreaCountryList(commit, reqList);
      return reqList || [];
    },
  }
}

function setAreaCountryList(commit, reqList) {
  // const areaMap = {
  //   Turkey: [34, 44, 45],
  //   BA: [18,19,29,35,25,4],
  //   NA: [8,23,1,33,21],
  //   WA: [27,17,40,41,39,14,20],
  //   SA: [12,26,5],
  //   EA: [22,13,28],
  //   nonLocalPrint: [],
  // }
  // const areaMap = {
  //   Turkey: [34, 44, 45],
  //   BA: [18,19,29,35,25,4],
  //   NA: [2,3,6,7,9,10,11,15,16,24,30,36,37,38,43,46,49],
  //   WA: [5,12,13,22,26,28,50,51,52,53,54],
  //   SA: [31,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70],
  //   EA: [1,4,8,14,17,18,19,20,21,23,25,27,29,32,33,35,39,40,41,42],
  //   // nonLocalPrint: [4,44,45,5,12,13,22,26,28,50,51,52,53,54,31,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70],
  //   nonLocalPrint: [1,2,3,4,6,7,8,9,10,11,14,15,16,17,18,19,20,21,23,24,25,27,29,30,32,33,35,36,37,38,39,40,41,42,43,46,47,48,49,65,68],
  //   taiyue: [47,48],
  // };
  // const nonLocalPrintName = ['BA', 'NA', 'WA', 'EA'];
  // for(let i = 0; i < nonLocalPrintName.length; i++) {
  //   areaMap.nonLocalPrint = areaMap.nonLocalPrint.concat(areaMap[nonLocalPrintName[i]]);
  // }
  // 0英语 1阿语 2印尼语 3土耳其语 4印地语 5越南语 6泰语 9西语
  const areaMap = {
    Turkey: [],
    BA: [18, 19, 29, 35, 25, 4],
    NA: [],
    WA: [],
    SA: [],
    EA: [],
    nonLocalPrint: [],
    taiyue: [],
  }
  const areaCountryList = {
    Turkey: [],
    BA: [],
    NA: [],
    WA: [],
    SA: [],
    EA: [],
    nonLocalPrint: [],
    taiyue: [],
    yuenan: [],
    portugal: [],
    tuyin: [],
    indonesia: [],
    All: [],
  };
  // for (let i = 0; i < reqList.length; i++) {
  //   const item = reqList[i];
  //   if (item.id === 0 || areaMap.Turkey.indexOf(item.id) !== -1) {
  //     areaCountryList.Turkey.push(item);
  //   }
  //   if (item.id === 0 || areaMap.BA.indexOf(item.id) !== -1) {
  //     areaCountryList.BA.push(item);
  //   }
  //   if (item.id === 0 || areaMap.NA.indexOf(item.id) !== -1) {
  //     areaCountryList.NA.push(item);
  //   }
  //   if (item.id === 0 || areaMap.WA.indexOf(item.id) !== -1) {
  //     areaCountryList.WA.push(item);
  //   }
  //   if (item.id === 0 || areaMap.SA.indexOf(item.id) !== -1) {
  //     areaCountryList.SA.push(item);
  //   }
  //   if (item.id === 0 || areaMap.EA.indexOf(item.id) !== -1) {
  //     areaCountryList.EA.push(item);
  //   }
  //   if (item.id === 0 || areaMap.nonLocalPrint.indexOf(item.id) !== -1) {
  //     areaCountryList.nonLocalPrint.push(item);
  //   }
  //   if (item.id === 0 || areaMap.taiyue.indexOf(item.id) !== -1) {
  //     areaCountryList.taiyue.push(item);
  //   }
  // }
  for (let i = 0; i < reqList.length; i++) {
    const item = reqList[i];
    // 土耳其
    if (item.id === 0 || item.country_region == 3) {
      areaCountryList.Turkey.push(item);
    }
    // 海湾(海湾的id是写死的)
    if (item.id === 0 || areaMap.BA.indexOf(item.id) !== -1) {
      areaCountryList.BA.push(item);
    }
    // 英语区
    if (item.id === 0 || item.country_region == 0) {
      areaCountryList.NA.push(item);
    }
    // 印地语区
    if (item.id === 0 || item.country_region == 4) {
      areaCountryList.WA.push(item);
    }
    // 西语区
    if (item.id === 0 || item.country_region == 9) {
      areaCountryList.SA.push(item);
    }
    // 阿语区
    if (item.id === 0 || item.country_region == 1) {
      areaCountryList.EA.push(item);
    }
    // 非土印西语区 除去土语区/印地语区/西语区的所有国家
    if (item.id === 0 || (item.country_region != 3 && item.country_region != 4 && item.country_region != 9)) {
      areaCountryList.nonLocalPrint.push(item);
    }
    // 泰越区
    if (item.id === 0 || item.id === 47 || item.id === 48 || item.id === 53 || item.country_region == 6) {
      areaCountryList.taiyue.push(item);
    }
    // 越南
    if (item.id === 0 || item.country_region == 5) {
      areaCountryList.yuenan.push(item);
    }
    // 西班牙
    // if (item.id === 0 || item.id == 46) {
    if (item.id === 0 || item.country_region == 11) {
      areaCountryList.portugal.push(item);
    }
    // 土印
    // if (item.id === 0 || item.country_region == 3 || item.country_region == 4) {
    if (item.id === 0 || item.country_region == 3 || item.country_region == 4 || item.country_region == 10) {
      areaCountryList.tuyin.push(item);
    }
    // 印尼
    if (item.id === 0 || item.country_region == 10) {
      areaCountryList.indonesia.push(item);
    }
    areaCountryList.All.push(item);
  }
  commit('setAreaCountryList', areaCountryList);
}
